<?php
class contest_upload_model extends MY_Model {
	private $table = 'contest_upload';
    private $module = 'contest_upload';
	
	function getsearchContent($limit,$page){
		$this->db->select('cli_contest_upload.*,cli_users.username ,cli_users.email,,cli_weeks_upload.name as nameweek');
		$this->db->limit($limit,$page);
        $this->db->from('cli_contest_upload');
        $this->db->join('cli_users', 'cli_users.id = cli_contest_upload.user_id',"left");
        $this->db->join('cli_weeks_upload', 'cli_weeks_upload.id = cli_contest_upload.week_id',"left");
		$this->db->order_by(PREFIX.$this->table.'.'.$this->input->post('func_order_by'),$this->input->post('order_by'));
        if($this->input->post("week_id") != -1)
        {
            $this->db->where('cli_contest_upload.week_id', $this->input->post("week_id"));
        }
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('`cli_users`.`username` LIKE "%'.$this->input->post('content'). '%" OR `cli_contest_upload`.`title` LIKE "%' . $this->input->post('content'). '%" OR `cli_contest_upload`.`like` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest_upload`.`fb_id` LIKE "%' . $this->input->post('content') .'%" OR `cli_users`.`email` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
        
		//$this->db->group_by(PREFIX.$this->table.'.id');
     
		$query = $this->db->get();

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
        $this->db->from('cli_contest_upload');
        $this->db->join('cli_users', 'cli_users.id = cli_contest_upload.user_id',"left");
        $this->db->join('cli_weeks_upload', 'cli_weeks_upload.id = cli_contest_upload.week_id',"left");
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
		  $this->db->where('`cli_users`.`username` LIKE "%'.$this->input->post('content'). '%" OR `cli_contest_upload`.`title` LIKE "%' . $this->input->post('content'). '%" OR `cli_contest_upload`.`like` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest_upload`.`fb_id` LIKE "%' . $this->input->post('content') .'%" OR `cli_users`.`email` LIKE "%'.$this->input->post('content').'%"');
		}
        if($this->input->post("week_id") != -1)
        {
            $this->db->where('cli_contest_upload.week_id', $this->input->post("week_id"));
        }
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
        
        $query = $this->db->count_all_results();

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$data = array(
				'content'=> $this->input->post('contentAdmincp'),
                'fb_id'=> $this->input->post('fb_idAdmincp'),
                'user_id'=> $this->input->post('usernameAdmincp'),
                'send_name'=> $this->input->post('send_nameAdmincp'),
                'receive_address'=> $this->input->post('send_addessAdmincp'),
				'thumb'=> $fileName['image'],
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			
			//Xử lý xóa hình khi update thay đổi hình
			if($fileName['image']==''){
				$fileName['image'] = $result[0]->thumb;
			}else{
				
                    @unlink(BASEFOLDER.DIR_UPLOAD_CONTEST.$result[0]->thumb);
                        
                            
			}
			//End xử lý xóa hình khi update thay đổi hình
			
			$data = array(
				'content'=> $this->input->post('contentAdmincp'),
                'fb_id'=> $this->input->post('fb_idAdmincp'),
                'user_id'=> $this->input->post('usernameAdmincp'),
                'send_name'=> $this->input->post('send_nameAdmincp'),
                'receive_address'=> $this->input->post('send_addessAdmincp'),
				'thumb'=> $fileName['image'],
				'status'=> $status,
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
    function get_list_weeks()
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $query = $this->db->get('cli_weeks_upload');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    
    
    function list_users() {
        $this->db->select('*');
        $this->db->where('status', 1);
        $query = $this->db->get(PREFIX . 'users');
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }
  
    
	function insert_user()
    {
        
         $fb_id = $_SESSION['fb_id'];
         $profile = $_SESSION['profile'] ;
         $gender ;
         if ($profile['gender'] == 'male')
         {
            $gender = 1;
         }
         else
         {
            $gender = 0;
         }
         $username = "";
         if($profile['username'])
         {
            $username = $profile['username'];
         }
         $data = array(
            'fb_id' => $fb_id,
            'email' =>$profile['email'],
            'username' => $username,
            'fullname' => $profile['name'],
            'gender' => $gender,
            'ip'=>getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
         if ($this->db->insert('cli_users', $data)) {
            return $this->db->insert_id();
        }
        return false;
        
    }
    function check_user($fb_id)
    {
        $this->db->select('id');
        $this->db->where('fb_id',$fb_id);
        $this->db->where('status', 1);
        $query = $this->db->get('cli_users');
        if ($query->num_rows() > 0) {
            return $query->num_rows();
        } else {
            return false;
        }
    }
    function get_user($fb_id)
    {
        $this->db->select('*');
        $this->db->where('fb_id',$fb_id);
        $this->db->where('status', 1);
        $query = $this->db->get('cli_users');
        if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	
	function list_name()
    {
        $this->db->select('fullname');
        $this->db->where('status', 1);
        $query = $this->db->get('cli_users');
        if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	
    function get_total_contest($week_id,$key = "")
    {
        $this->db->select('cli_contest_upload.*,u.username,u.fullname,u.email');
        $this->db->where('cli_contest_upload.status', 1);
        if($week_id != 0)
        {
            $this->db->where('cli_contest_upload.week_id', $week_id);
            
        }
        if($key !="")
        {
            $this->db->like('u.username',$key);
        }
        $this->db->from(PREFIX.'contest_upload');
       	$this->db->join(PREFIX.'users AS u','u.id = cli_contest_upload.user_id','left');
        $query = $this->db->count_all_results();

		if($query > 0){
			return $query;
		}else{
			return false;
		}
    }
    function list_contest($week_id = 0,$page = 0,$key = "")
    {
        $this->db->select('cli_contest_upload.*,u.username,u.fullname,u.email');
        $this->db->where('cli_contest_upload.status', 1);
        if($week_id != 0)
        {
            $this->db->where('cli_contest_upload.week_id', $week_id);
            
        }
        if($page==0)
        {
            $page = 0;
        }
        
        if($key !="")
        {
            $this->db->like('u.fullname',$key);
        }
        $this->db->limit(4, $page * 4);
        $this->db->from(PREFIX.'contest_upload');
       	$this->db->join(PREFIX.'users AS u','u.id = cli_contest_upload.user_id','left');
        $this->db->order_by("cli_contest_upload.created","desc");
        $query = $this->db->get();        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }
	
	
    function list_contest_seach($key,$week_id)
    {
        $this->db->select('cli_contest_upload.*,u.username,u.fullname,u.email');
        $this->db->where('cli_contest_upload.status', 1);
        if($week_id != -1)
        {
            $this->db->where('cli_contest_upload.week_id', $week_id);
        }
        $this->db->like('u.username',$key);
        $this->db->from(PREFIX.'contest_upload');
       	$this->db->join(PREFIX.'user AS u','u.id = contest_upload.user_id','left');
        $this->db->order_by("cli_contest_upload.created","desc");
        $query = $this->db->get();        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }
    function getContestDetail($contest_id)
	{
		$this->db->select('c.*,u.fb_id as fbid,u.username,u.fullname,u.email');
		$this->db->from(PREFIX.'contest_upload AS c');
		$this->db->join(PREFIX.'users AS u','u.id = c.user_id','left');
		$this->db->where('c.status',1);
		$this->db->where('c.id',$contest_id);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			return $query->row();
		}
		return false;
	}

    function list_contest_winner()
    {
        $this->db->select('cli_contest_upload.*,cli_users.fullname');
        $this->db->where('cli_contest_upload.status', 1);
        $this->db->where('cli_contest_upload.is_winner', 1);
        $this->db->from(PREFIX.'contest');
		$this->db->join(PREFIX.'users','cli_users.id = cli_contest_upload.user_id','left');
		$this->db->order_by("cli_contest_upload.created","desc");
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }
    function get_profile($fb_id)
    {
        $this->db->select('*');
       	$this->db->where('fb_id',$fb_id);
        $query = $this->db->get('cli_users');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }

    function insert_like($fb_id,$user_id,$contest_id)
    {
        $data = array(
            'fb_id' => $fb_id,
            'contest_id' =>$contest_id,
            'user_id' => $user_id,
            'ip'=>getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
         if ($this->db->insert('cli_contest_upload_like', $data)) {
            return $this->db->insert_id();
        }
        return false;
    }
    function is_like($uid,$contest_id)
	{
		$this->db->select('id');
		$this->db->where('contest_id',$contest_id);
		$this->db->where('user_id',$uid);
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.'contest_upload_like');
		
		if($query->num_rows() > 0)
		{
			return 1;
		}
		return 0;
	}
	function is_like_ip($ip,$contest_id)
	{
		$this->db->select('id');
		$this->db->where('contest_id',$contest_id);
		$this->db->where('ip',$ip);
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.'contest_upload_like');
		
		if($query->num_rows() > 0)
		{
			return count($query->result());
		}
		return 0;
	}

    function update_like($like,$id)
	{
		$this->db->where('id',$id);
        $data = array('like'=>$like);
		if($this->db->update("cli_contest_upload",$data))
		{
			return true;
		}
		return false;
	}
    function incre_view($view,$contest_id)
	{
		$this->db->where('id',$contest_id);
		$data = array('view'=>$view);
		if($this->db->update('cli_contest_upload',$data))
		{
			return true;
		}
		return false;
	}
    function incre_comment($comment,$contest_id)
	{
		$this->db->where('id',$contest_id);
		$data = array('comment'=>$comment);
		if($this->db->update('cli_contest_upload',$data))
		{
			return true;
		}
		return false;
	}
   
    function get_weeks()
    {
        $this->db->select('*');
        $this->db->where('active',1);
        $this->db->where('status',1);
        $this->db->limit(1);
        $query = $this->db->get('cli_weeks_upload');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	
    function list_weeks()
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->from('cli_weeks');
	    $query = $this->db->count_all_results();
		if($query > 0){
			return $query;
		}else{
			return false;
		}
    }
    function winner_weeks()
    {
        $this->db->select('cli_weeks_upload.week_id');
        $this->db->from('cli_weeks_upload');
        $this->db->where('cli_weeks_upload.status',1);
        $this->db->group_by('week_id');
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
   	function detail_weeks($id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('id',$id);
        $query = $this->db->get('cli_weeks_upload');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function get_comments($contest_id)
	{
		$this->db->select('c.*,u.username,u.email');
		$this->db->from(PREFIX.'contest_upload_comment AS c');
		$this->db->join(PREFIX.'users AS u','u.id = c.user_id','LEFT');
		$this->db->where('c.status',1);
		$this->db->where('c.contest_id',$contest_id);
		$this->db->order_by('c.created','DESC');
		$query = $this->db->get();
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}
    function get_count_comments($contest_id)
	{
		$this->db->select('id');
		$this->db->where('status',1);
		$this->db->where('contest_id',$contest_id);
		$query = $this->db->get(PREFIX."contest_upload_comment");
		if($query->num_rows() > 0)
		{
			return count($query->result());
		}
		return 0;
	}
	
	function count_likes($contest_id)
	{
		$this->db->select('id');
		$this->db->where('contest_id',$contest_id);
		$this->db->where('status',1);
		$query = $this->db->get('cli_contest_upload_like');
		if($query->num_rows() > 0)
		{
			return count($query->result());
		}
		return false;
	}
	
  function list_winner_weeks($week_id)
    {
        $this->db->select('cli_contest_upload.*,cli_users.username ,cli_users.email');
        $this->db->from('cli_contest_upload');
        $this->db->join('cli_users', 'cli_users.id = cli_contest_upload.user_id',"left");
        $this->db->limit(10);
        $this->db->where('cli_contest_upload.status',1);
        $this->db->where('cli_contest_upload.is_winner',1);
        $this->db->where('cli_contest_upload.week_id',$week_id);
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	function list_winner_weeks_pizza($week_id)
    {
        $this->db->select('cli_contest_upload.*,cli_users.username ,cli_users.email');
        $this->db->from('cli_contest_upload');
        $this->db->join('cli_users', 'cli_users.id = cli_contest_upload.user_id',"left");
        $this->db->limit(10);
        $this->db->where('cli_contest_upload.status',1);
        $this->db->where('cli_contest_upload.winner_choose',1);
        $this->db->where('cli_contest_upload.week_id',$week_id);
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
   

}